<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateMeetingOverviewAPIRequest;
use App\Http\Requests\API\UpdateMeetingOverviewAPIRequest;
use App\Models\MeetingOverview;
use App\Repositories\MeetingOverviewRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class MeetingOverviewController
 * @package App\Http\Controllers\API
 */
class MeetingOverviewAPIController extends AppBaseController
{
    /** @var  MeetingOverviewRepository */
    private $meetingOverviewRepository;

    /**
     * Store a newly created MeetingOverview in storage.
     * POST /meetingOverviews
     *
     * @param CreateMeetingOverviewAPIRequest $request
     *
     * @return Response
     */
    public function store(MeetingOverviewRepository $meetingOverviewRepository)
    {
        $input = $this->request->all();
        $this->validateInputParams('store', $input);

        $meetingOverviews = $meetingOverviewRepository->create($input);

        return $this->sendResponse($meetingOverviews->toArray(), 'Meeting Overview saved successfully');
    }

    /**
     * display_order asc
     * @param TicketModelRepository $repo
     */
    public function apiSearch($id, MeetingOverviewRepository $repo)
    {
        $searchOptions = $this->normalizeSearchOptions();
        $sortOptions = $this->normalizeSortOptions();
        $searchOptions['status'] = 0;
        $input = $this->request->all();
        $searchOptions['meeting_id'] = $id;
        // mock
        $sortOptions['sort_key'] = 'created_at';
        $sortOptions['sort'] = 'desc';

        $ret = $repo->search($searchOptions, $sortOptions);

        $lists = $ret['lists'];
        if (count($lists) == 0) {
            return $this->success(['lists' => []]);
        }

        $ret['lists'] = $lists;

        return $this->success($ret);
    }

    /**
     * display_order asc
     * @param TicketModelRepository $repo
     */
    public function search(MeetingOverviewRepository $repo)
    {
        $searchOptions = $this->normalizeSearchOptions();
        $sortOptions = $this->normalizeSortOptions();
        $searchOptions['status'] = 0;
        $input = $this->request->all();
        $searchOptions['meeting_id'] = $input['meeting_id'];
        $ret = $repo->search($searchOptions, $sortOptions);

        $lists = $ret['lists'];
        if (count($lists) == 0) {
            return $this->success(['lists' => []]);
        }

        $ret['lists'] = $lists;

        return $this->success($ret);
    }

    /**
     * Display the specified MeetingOverview.
     * GET|HEAD /meetingOverviews/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id, MeetingOverviewRepository $meetingOverviewRepository)
    {
        /** @var MeetingOverview $meetingOverview */
        $meetingOverview = $meetingOverviewRepository->findWithoutFail($id);

        if (empty($meetingOverview)) {
            return $this->sendError('Meeting Overview not found');
        }

        return $this->sendResponse($meetingOverview->toArray(), '成功');
    }

    /**
     * Update the specified MeetingOverview in storage.
     * PUT/PATCH /meetingOverviews/{id}
     *
     * @param  int $id
     * @param UpdateMeetingOverviewAPIRequest $request
     *
     * @return Response
     */
    public function update($id, MeetingOverviewRepository $meetingOverviewRepository)
    {
        $input = $this->request->all();
        $this->validateInputParams('store', $input);

        /** @var MeetingOverview $meetingOverview */
        $meetingOverview = $meetingOverviewRepository->findWithoutFail($id);

        if (empty($meetingOverview)) {
            return $this->sendError('Meeting Overview not found');
        }

        $meetingOverview->updateRecord($id, $input);

        return $this->sendResponse($meetingOverview->toArray(), 'MeetingOverview updated successfully');
    }

    /**
     * Update the specified MeetingOverview in storage.
     * PUT/PATCH /meetingOverviews/{id}
     *
     * @param  int $id
     * @param UpdateMeetingOverviewAPIRequest $request
     *
     * @return Response
     */
    public function batchUpdate($id,  MeetingOverviewRepository $meetingOverviewRepository)
    {
        $input = $this->request->all();
        //$this->validateInputParams('store', $input);
        $payload = $input['lists'];
        if (empty($payload)) {
            return $this->error(1, "编辑内容不存在");
        }

        $payloads = json_decode($payload, true);
        foreach ($payloads as $payload) {
            $id = $payload['id'];
            /** @var MeetingOverview $meetingOverview */
            $meetingOverview = $meetingOverviewRepository->findWithoutFail($id);
            if (empty($meetingOverview) || $meetingOverview->status != 0) {
                return $this->sendError('Meeting Overview not found');
            }

            if ($meetingOverview->meeting_id != $id) {
                return $this->sendError('Meeting Overview not found');
            }

            $meetingOverview->updateRecord($id, $input);
        }

        return $this->sendResponse([], '批量修改成功');
    }

    /**
     * Remove the specified MeetingOverview from storage.
     * DELETE /meetingOverviews/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var MeetingOverview $meetingOverview */
        $meetingOverview = $this->meetingOverviewRepository->findWithoutFail($id);

        if (empty($meetingOverview)) {
            return $this->sendError('Meeting Overview not found');
        }

        $meetingOverview->delete();

        return $this->sendResponse($id, 'Meeting Overview deleted successfully');
    }

    /**
     * Remove the specified AdminModel from storage.
     * DELETE /adminModels/{id}
     *
     * @param  int $idbatch
     *
     */
    public function batchDestroy()
    {
        $ids = $this->request->get('ids');
        if (empty($ids)) {
            return $this->sendError('id不为空');
        }

        $ids = explode(',', $ids);
        // 删除ticket
        MeetingOverview::whereIn('id', $ids)->update(['status' => 2]);

        return $this->success([], '操作成功');
    }

    /**
     * 参数验证规则
     * self::VALID_RULES[__FUNCTION__]
     */
    const VALID_RULES = [
        'store' => [
            'meeting_id' => 'required|int|min:1',
            'display_order' => 'int|min:0',
            'article' => 'required|string',
            'name' => 'required|string',
            'english_name' => 'string',
            'cover' => 'required|string',
        ],
        'update' => [
            'meeting_id' => 'int|min:10|max:12',
            'display_order' => 'int|min:0',
            'status' => 'int|min:0|max:2',
            'article' => 'string',
            'name' => 'string',
            'english_name' => 'string',
            'cover' => 'string',
        ],
    ];
}
